package dk.simonvogensen.itodo.client.view;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;
import dk.simonvogensen.itodo.client.controller.Controller;
import dk.simonvogensen.itodo.client.model.ProjectId;
import dk.simonvogensen.itodo.client.model.TransportType;
import dk.simonvogensen.itodo.shared.model.Config;
import dk.simonvogensen.uirecorder.client.RecordableButton;
import dk.simonvogensen.uirecorder.client.RecordableFlexTable;
import dk.simonvogensen.uirecorder.client.RecordableLabel;
import dk.simonvogensen.uirecorder.client.RecordableVerticalPanel;
import org.axed.user.client.DOM;

import java.util.Map;

/**
 * @author $LastChangedBy:$ $LastChangedDate:$
 * @version $Revision:$
 */
public class WorktimeDialogBox extends DialogBox {

    public WorktimeDialogBox(Controller controller, final View view) {
        super(false, false);
        setText("Worktime");
        VerticalPanel panel = new RecordableVerticalPanel();

        Button xmlButton = new RecordableButton("XML", new ClickHandler() {
            public void onClick(ClickEvent event) {
                view.openXmlWorktimeDialog();
            }
        });
        panel.add(xmlButton);

        FlexTable table = new RecordableFlexTable();
        DOM.setStyleAttribute(table.getElement(), "margin", "0px");
        DOM.setStyleAttribute(table.getElement(), "borderCollapse", "collapse");

        table.setWidget(0, 0, new RecordableLabel("date"));
        DOM.setStyleAttribute(table.getCellFormatter().getElement(0, 0), "border", "1px solid black");

        table.setWidget(0, 1, new RecordableLabel("total"));
        DOM.setStyleAttribute(table.getCellFormatter().getElement(0, 1), "border", "1px solid black");

        int i = 2;
        Config config = controller.getConfig();
        for (ProjectId projectId : config.getProjectIdList()) {
            table.setWidget(0, i, new RecordableLabel(projectId.getName()));
            DOM.setStyleAttribute(table.getCellFormatter().getElement(0, i), "border", "1px solid black");
            i++;
        }
        int row = 1;
        for (String[] strings : controller.getWorktimeList()) {
            table.setWidget(row, 0, new RecordableLabel(strings[0]));
            DOM.setStyleAttribute(table.getCellFormatter().getElement(row, 0), "border", "1px solid black");
            table.setWidget(row, 1, new RecordableLabel(strings[1]));
            DOM.setStyleAttribute(table.getCellFormatter().getElement(row, 1), "border", "1px solid black");
            i = 2;
            for (ProjectId projectId : config.getProjectIdList()) {
                table.setWidget(row, i, new RecordableLabel(strings[i]));
                DOM.setStyleAttribute(table.getCellFormatter().getElement(row, i), "border", "1px solid black");
                i++;
            }
            row++;
        }
        table.setWidth("95%");
        panel.add(table);
        panel.setCellHorizontalAlignment(table, HorizontalPanel.ALIGN_CENTER);

        Map<TransportType,Integer> transportTypeMap = controller.getTransportTypeMap();
        for (TransportType transportType : transportTypeMap.keySet()) {
            panel.add(new Label(transportType.getName()+": "+transportTypeMap.get(transportType)));
        }

        Button okButton = new RecordableButton("Close", new ClickHandler() {
            public void onClick(ClickEvent event) {
                WorktimeDialogBox.this.hide();
            }
        });
        panel.add(okButton);
        panel.setCellHorizontalAlignment(okButton, HorizontalPanel.ALIGN_CENTER);
        setWidget(panel);
    }
}

